(function() {
    easyDialog = function(para) {
        this.tpl = {
            DHTML: '<div id="ed-dialog">' + //弹窗本体的HTML
                '<div class="ed-wrap">' +
                '<div class="ed-title"></div>' +
                '<div class="ed-content"></div>' +
                '<div class="ed-btns"></div>' +
                '</div>' +
                '</div>',
            OLHTML: '<div id="ed-overlay"></div>' //背景蒙版的HTML
        };
        this.model = para.model;
        this.title = para.title;
        this.content = para.content;
        this.buttons = para.buttons;
        this.bg_click = para.bg_click;
        this._init();
    }

    easyDialog.prototype = {
        _init: function() {
            var Domlist = document.querySelectorAll('#ed-dialog');
            if (Domlist.length < 1) { //只允许存在一个弹框
                //假如没有生成模板
                $('body').prepend(this.tpl.DHTML); //插入主体框架
                $('body').prepend(this.tpl.OLHTML); //插入背景蒙版
            }
            var Dobj = this;
            $(this).on('ready', function() {
                //初始化内容
                if (Dobj.title !== null) {
                    $('#ed-dialog').find('.ed-title').html(Dobj.title);
                } else {
                    $('#ed-dialog').find('.ed-title').html('');
                }
                if (Dobj.content !== null) {
                    $('#ed-dialog').find('.ed-content').html(Dobj.content);
                } else {
                    $('#ed-dialog').find('.ed-content').html('');
                }

                //初始化按钮
                if (Dobj.buttons !== null) {
                    var Bhtml = '';
                    var Cname, i = 0,
                        id;
                    for (key in Dobj.buttons) {
                        id = i.toString();
                        Cname = 'ed-btn-' + id;
                        i++;
                        Bhtml += '<button class="ed-btn ' + Cname + '">' + key + '</button>';
                    }
                    $('#ed-dialog').find('.ed-btns').html(Bhtml);
                } else {
                    $('#ed-dialog').find('.ed-btns').html('<button class="ed-btn">确认</button>');
                }

                //初始化效果
                var model = 'ed-effect-' + Dobj.model.toString();
                $('#ed-dialog').addClass(model);
                Dobj._eventHandler();
            });
        },
        show: function() {
            $("#ed-dialog").addClass('ed-show');
            $("#ed-overlay").addClass('ed-show');
        },
        hide: function() {
            $('body').unbind('touchmove');
            $('#ed-dialog .ed-btns button').unbind('click');
            $("#ed-dialog").removeClass('ed-show');
            $("#ed-overlay").removeClass('ed-show');
            setTimeout(function() {
                $("#ed-dialog").remove();
                $("#ed-overlay").remove();
            }, 300);
        },
        _eventHandler: function() {

            var Dobj = this;
            $('body').one('touchmove', function(event) {
                event.stopPropagation();
                event.preventDefault();
            });
            if (this.buttons == null) {
                $('#ed-dialog').find('.ed-btns button').one('click', function(event) {
                    event.stopPropagation();
                    Dobj.hide();
                });
            } else {
                var Bfunc = null,
                    Cname = null,
                    i = 0;
                for (key in this.buttons) {
                    Bfunc = this.buttons[key];
                    id = i.toString();
                    Cname = '#ed-dialog .ed-btns .ed-btn-' + id;
                    i++;
                    $(Cname).one('click', $.proxy(Bfunc, Dobj));
                    //$(document).one('click', Cname, $.proxy(Bfunc, Dobj));
                }
            }
        }
    };
})();


function easy_dialog() {
    var EDo = null;
    var oDA = { //object default arguments
        model: "3",
        title: null,
        content: "",
        buttons: null,
        bg_click: true
    }
    if (arguments.length > 0) {
        if ("undefined" != typeof(arguments[0].model)) {
            oDA.model = arguments[0].model;
        };
        if ("undefined" != typeof(arguments[0].title)) {
            oDA.title = arguments[0].title;
        };
        if ("undefined" != typeof(arguments[0].content)) {
            oDA.content = arguments[0].content;
        };
        if ("undefined" != typeof(arguments[0].buttons)) {
            oDA.buttons = arguments[0].buttons;
        };
        if ("undefined" != typeof(arguments[0].bg_click)) {
            oDA.bg_click = arguments[0].bg_click;
        };
    };
    EDo = new easyDialog(oDA); //easy dialog object
    setTimeout(function () {
        EDo.show();
    }, 300);
    return EDo;
}

//ajax 封装

(function() {
    __ajax_request_cache = new Array();
    var async = true;
    var abort_type = 'drop';
    var type = 'POST';
    _ajax = function(param) {
        var mask = false;
        if (param.mask != null) mask = param.mask;
        if (param.length < 2) return false;
        var url = param.url;
        var data = param.data;
        var callback = param.callback;
        var type = param.type || type;
        if (null !== param.idx && undefined !== param.idx) {
            var idx = param.idx;
        } else {
            var idx = url;
            try {
                /*兼容性设置*/
                e = arguments[6];
                e = e || event;
                sE = (e.srcElement || e.target);
                if (e != null && sE.id != null && typeof(e) != 'undefined' && typeof(sE.id) != 'undefined') {
                    idx += sE.id;
                } else {
                    idx += random(0, 99999); //随机数
                }
            } catch (error) {
                idx += random(0, 99999); //随机数
            }
        }
        if (null !== param.async && undefined !== param.async)
            async = !!(param.async);
        if (null !== param.abort_type && undefined !== param.abort_type) {
            switch (param.abort_type) {
                case 'abort':
                    abort_type = 'abort';
                    break;
                case 'drop':
                    abort_type = 'drop';
                    break;
                default:
                    abort_type = 'drop';
                    break;
            }
        };
        if (undefined !== __ajax_request_cache[idx]) {
            if (abort_type == 'abort') {
                __ajax_request_cache[idx].abort();
            } else {
                return true;
            }
        };
        __ajax_request_cache[idx] = $.ajax({
            url: url,
            type: type,
            dataType: 'json',
            timeout: 5000,
            data: data,
            idx: idx,
            mask: mask,
            callback: callback
        });
    }
    $(document).on('ajaxBeforeSend', function(e, xhr, o) {
        if (o.mask == true) {
            $('body').prepend('<div class="mask"><div class="loading"></div></div>');
            $('div.loading').css('top', screen.height / 2);
            $('div.loading').css('left', screen.width / 2);
        }
    });
    $(document).on('ajaxError', function(e, xhr, o) {
        if (o.mask == true) {
            try {
                easy_dialog({
                    content: '请检查您的网络连接'
                });
            } catch (e) {
                alert('请检查您的网络连接');
            }
        }
    });
    $(document).on('ajaxSuccess', function(e, xhr, o) {
        if (o.callback !== undefined) {
            var response = $.parseJSON(xhr.responseText);
            o.callback(response);
        };
    });
    $(document).on('ajaxComplete', function(e, xhr, o) {
        if (o.idx !== undefined) {
            delete __ajax_request_cache[o.idx];
        };
        $('div.mask').remove();
    });
})();
